
local log = require("core.log")
local path = require("core.path")
local yaml = require("tinyyaml")
local io_open = io.open
local type = type
local str_gmatch = string.gmatch
local string = string
local pairs = pairs
local getmetatable = getmetatable

-----nginx目录下的 openresty/conf/config.yaml
local local_default_conf_path = path:yaml_path("config") --默认的配置
print("local_default_conf_path",local_default_conf_path)
local local_conf_path = path:yaml_path("config")
print("local_conf_path",local_conf_path)
local config_data


local _M = {}


local function read_file(path)
    print("read file "..path)
    local file, err = io_open(path, "rb")   -- 读取文件
    if not file then
        log.error("failed to read config file:" .. path, ", error info:", err)
        return nil, err
    end

    local content = file:read("*a") -- `读取整个文件
    file:close()
    return content
end

function _M.clear_cache()
    config_data = nil
end


local function is_empty_yaml_line(line)
    return line == '' or string.find(line, '^%s*$') or
            string.find(line, '^%s*#')
end


local function tab_is_array(t)
    local count = 0
    for k,v in pairs(t) do
        count = count + 1
    end

    return #t == count
end


local function tinyyaml_type(t)
    local mt = getmetatable(t)
    if mt then
        log.debug("table type: ", mt.__type)
        return mt.__type
    end
end


local function merge_conf(base, new_tab, ppath)
    --print("======> merge")
    ppath = ppath or ""

    for key, val in pairs(new_tab) do
        print("merge_conf key:",key)
        if type(val) == "table" then
            if tinyyaml_type(val) == "null" then
                base[key] = nil

            elseif tab_is_array(val) then
                base[key] = val

            else
                if base[key] == nil then
                    base[key] = {}
                end

                local ok, err = merge_conf(
                        base[key],
                        val,
                        ppath == "" and key or ppath .. "->" .. key
                )
                if not ok then
                    return nil, err
                end
            end
        else
            if base[key] == nil then
                base[key] = val
            elseif type(base[key]) ~= type(val) then
                return false, "failed to merge, path[" ..
                        (ppath == "" and key or ppath .. "->" .. key) ..
                        "] expect: " ..
                        type(base[key]) .. ", but got: " .. type(val)
            else
                base[key] = val
            end
        end
    end

    return base
end
----读取本地文件配置  force 可以不传值
function _M.merge_conf(base, new_tab)
    merge_conf(base, new_tab, "");
end
----读取本地文件配置  force 可以不传值
function _M.local_conf(force)

    if not force and config_data then
        print("===============已有配置=================")

        if  config_data["c"] ~= nil then
            print("config_data的c值"..config_data["c"]);
        end

        return config_data
    end
    print("===============重新读取了=================")
    local default_conf_yaml, err = read_file(local_default_conf_path)
    if type(default_conf_yaml) ~= "string" then
        return nil, "failed to read config-default file:" .. err
    end

    config_data = yaml.parse(default_conf_yaml)




    --print("读取完毕配置文件");
    --print(config_data.my.b)
    return config_data

end


return _M
